package com.bike.controller.system;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bike.controller.BaseController;
import com.bike.entity.SysDictionaries;
import com.bike.entity.SysMenu;
import com.bike.util.PageData;
import com.bike.service.impl.SysDictionariesServiceImpl;

 
@Controller
@RequestMapping(value="/sysDictionaries")
public class SysDictionariesController extends BaseController {
	
		@Autowired
		private SysDictionariesServiceImpl  sysDictionariesService;
	
		/**
		 * 跳转到列表页面
		 */
		@RequestMapping("toList")
		public String toList(){
			return "/system/dictionaries/sysDictionaries_list";
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("list")
		@ResponseBody
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysDictionariesService.selectSysDictionariesList(pd);
		}
		/**
		 * 列表查询(或根据条件)
		 */
		@RequestMapping("listAll")
		@ResponseBody
		public PageData listAll(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysDictionariesService.selectSysDictionariesList(pd);
		}
		/**
		 * 跳转到编辑页面（新增和修改）
		 */
		@RequestMapping("toEdit")
		public String toEdit(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			if(pd.getLong("dictionariesId")>0){
				model.addAttribute("pd", sysDictionariesService.selectBySysDictionariesId(pd.getLong("dictionariesId")));
			} 
			model.addAttribute("dictionariesTree", sysDictionariesService.selectByParentId(0));
			return "/system/dictionaries/sysDictionaries_edit";
		}
		/**
		 * 新增和修改 
		 */
		@RequestMapping("save")
		@ResponseBody
		 public Map<String,Object> save(HttpServletRequest request,SysDictionaries sysDictionaries){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				sysDictionariesService.saveOrUpdate(pd,sysDictionaries);
				return getSuccessMap();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		 }
		/**
		 * 删除
		 */
	 	@RequestMapping("delete")
		@ResponseBody
		 public Map<String,Object> delete(HttpServletRequest request){
			try {
				PageData pd=PageData.getPageDataByPage(request);
				if(pd.getLong("dictionariesId")>0){
					sysDictionariesService.deleteSysDictionariesById(pd.getLong("dictionariesId"));
					return getSuccessMap();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			return getErrorMap("失败");
		}
	 	/**
	 	 * 根据父级查找
	 	 * @param parentId
	 	 * @return
	 	 */
	 	@RequestMapping("listByParent")
		@ResponseBody
		public List<SysDictionaries> listByParent(HttpServletRequest request){
	 		PageData pd=PageData.getPageDataByPage(request);
			long parentId=pd.getLong("id");
			 List<SysDictionaries> sysDictionaries=sysDictionariesService.selectByParentId(parentId);
			return sysDictionaries ;
		}
	 	/**
		 * 详细信息
		 */
		@RequestMapping("detail")
		@ResponseBody
		public SysDictionaries detail(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			return sysDictionariesService.selectBySysDictionariesId(pd.getLong("dictionariesId"));
		}
}
